package com.liukai.todo_android.presenter;

import android.os.Build;

import androidx.annotation.RequiresApi;

import com.liukai.todo_android.call_back.IUpdateScheduleCallBack;
import com.liukai.todo_android.common.api.TodoAPI;
import com.liukai.todo_android.common.base.BasePresenter;
import com.liukai.todo_android.common.constants.ScheduleConst;
import com.liukai.todo_android.common.utils.Constants;
import com.liukai.todo_android.common.utils.DateUtil;
import com.liukai.todo_android.common.utils.RetrofitManager;
import com.liukai.todo_android.entity.ItemVo;
import com.liukai.todo_android.entity.Resp;
import com.liukai.todo_android.entity.vo.UpdateItemVo;

import java.net.HttpURLConnection;
import java.util.Date;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * @author ：liukai
 * @date ：Created in 2021/8/16 2:31 下午
 * @description：修改待办事项的相关请求
 * @modified By：
 * @version: $
 */
public class UpdateSchedulePresenter extends BasePresenter<IUpdateScheduleCallBack> {

    /**
     * 设为已完成
     *
     * @param itemVo
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public void setScheduleFinish(ItemVo itemVo) {
        setItemFinish(itemVo.getId());
    }

    /**
     * 设为未完成
     *
     * @param itemVo
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public void setScheduleNotFinish(ItemVo itemVo) {
        setItemNotFinish(itemVo.getId());
    }

    /**
     * 修改
     *
     * @param updateItemVo
     */
    public void update(UpdateItemVo updateItemVo) {
        updateSchedule(updateItemVo);
    }

    public void saveConcentration(ItemVo itemVo) {
        UpdateItemVo updateItemVo = UpdateItemVo.build(itemVo);
        updateSchedule(updateItemVo);
        setItemFinish(itemVo.getId());
    }

    /**
     * 设为已完成
     *
     * @param id
     */
    private void setItemFinish(Long id) {
        Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
        TodoAPI todoAPI = retrofit.create(TodoAPI.class);
        Call<Resp> finishTask = todoAPI.setItemFinish(id);
        finishTask.enqueue(new Callback<Resp>() {
            @Override
            public void onResponse(Call<Resp> call, Response<Resp> response) {
                if (response.code() == HttpURLConnection.HTTP_OK) {
                    requestSuccess(response);
                } else {
                    requestFailure(response);
                }
            }

            @Override
            public void onFailure(Call<Resp> call, Throwable t) {
                requestFailure(t);
            }
        });
    }

    /**
     * 设为未完成
     *
     * @param id
     */
    private void setItemNotFinish(Long id) {
        Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
        TodoAPI todoAPI = retrofit.create(TodoAPI.class);
        Call<Resp> finishTask = todoAPI.setItemNotFinish(id);
        finishTask.enqueue(new Callback<Resp>() {
            @Override
            public void onResponse(Call<Resp> call, Response<Resp> response) {
                if (response.code() == HttpURLConnection.HTTP_OK) {
                    requestSuccess(response);
                } else {
                    requestFailure(response);
                }
            }

            @Override
            public void onFailure(Call<Resp> call, Throwable t) {
                requestFailure(t);
            }
        });
    }

    /**
     * 修改待办事项
     *
     * @param updateItemVo
     */
    private void updateSchedule(UpdateItemVo updateItemVo) {
        Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
        TodoAPI todoAPI = retrofit.create(TodoAPI.class);
        Call<Resp> updateTask = todoAPI.updateSchedule(updateItemVo);
        updateTask.enqueue(new Callback<Resp>() {
            @Override
            public void onResponse(Call<Resp> call, Response<Resp> response) {
                if (response.code() == HttpURLConnection.HTTP_OK) {
                    requestSuccess(response);
                } else {
                    requestFailure(response);
                }
            }

            @Override
            public void onFailure(Call<Resp> call, Throwable t) {
                requestFailure(t);
            }
        });
    }

    private void requestFailure(Throwable t) {
        mCallBack.updateScheduleError(Resp.err(ScheduleConst.ADD_SCHEDULE_FAILED_NO_INTERNET));
    }

    private void requestFailure(Response<Resp> response) {
        mCallBack.updateScheduleError(Resp.err(ScheduleConst.ADD_SCHEDULE_FAILED));
    }

    private void requestSuccess(Response<Resp> response) {
        Resp body = response.body();
        if (body.getCode() == ScheduleConst.UPDATE_SCHEDULE_SUCCESS.getCode()) {
            mCallBack.updateScheduleSuccess(body);
        } else {
            mCallBack.updateScheduleError(Resp.err(ScheduleConst.UPDATE_SCHEDULE_FAILED));
        }
    }

}
